/*
 * Copyright (c) 2020, 2023, 3044483124@qq.com All rights reserved.
 *
 */
package cn.itcast;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>Project: JavaStudy - WordCound
 * <p>Powered by jiangbo On 2023-01-12
 * 14:26:43
 * <p>Created by IntelliJ IDEA
 *
 * @author jiangbo [3044483124@qq.com]
 * @version 1.0
 * @since 8/17
 */
public class WordCound {
    public static void main(String[] args) {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader("user.txt"));
            StringBuffer sb = new StringBuffer();
            String str = null;
            while((str = br.readLine()) != null) {
                sb.append(str);
            }

            String regex = "z";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(sb);

            int num = 0;
            while(matcher.find()) {
                num++;
            }

            System.out.println(regex+"出现的次数为：" + num);

        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(null != br) {
                    //关闭资源
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}


